function choosepath(cmd, startpath)

global pathdone
global selpath

switch(cmd)
   
case 'newpath'
   hl = findobj('Tag', 'PathList'); % find the selected path
   pl = get(hl, 'Value');
   pltxt = get(hl, 'String');
   [p f e] = fileparts(pltxt{pl});
   if(isempty(f) | strcmp(f, '..'))
      pltxt{pl} = upone(p);
   end;
   selpath = pltxt{pl};
   if(selpath(end) ~= '\')
      selpath(end+1) = '\';
   end;
   s
   choosepath('init', selpath);
   return;
   
case 'init'
   if(exist(startpath,'dir') ~= 7)
      msgbox(sprintf('choosepath: path %s does exist', startpath));
      return;
   end;
   x = dir(startpath);
   pathlist = {};
   k = 1;
   for i = 1:length(x)
      if(x(i).isdir & ~strcmp(x(i).name, '.'))
         pathlist{k} = [startpath x(i).name]; % pick out the directories.
         k = k + 1;
      end;
   end;
   pathlist = sort(pathlist);
   h = findobj('Tag', 'PathChooser'); % make it modal.
   if(isempty(h))
      pathdone = 0;
      pathchooser;
      h = findobj('Tag', 'PathChooser');
   end;
   hl = findobj('Tag', 'PathList');
   if(~isempty(pathlist))
      set(hl, 'String', pathlist);
   else
      set(hl, 'String', {'..'});
   end;
      set(hl, 'Value', 1);
   
   drawnow;
   
case 'cancel'
   disp('cancel')
   h = findobj('Tag', 'PathChooser');
   delete(h);
   pathdone = 1;
   return;
   
   
   
case 'ok'
   disp('ok')
   hl = findobj('Tag', 'PathList');
   pl = get(hl, 'Value');
   pltxt = get(hl, 'String');
   selpath = pltxt{pl}; 
   h = findobj('Tag', 'PathChooser');
   delete(h);
   pathdone = 1;
   return;
   
otherwise
   return;
end;

function [po] = upone(p)

[px, po] = strtok(fliplr(p), '\');
if(isempty(po))
   po = p;
else
   po = fliplr(po);
end;
